package api

import (
	"service"
	"github.com/gin-gonic/gin"
	"pojo/form"
	"net/http"
	"strconv"
	"pojo/vo"
	"constant"
)

type UserApi struct {
	service *service.UserService
}

// 创建UserApi
func NewUserApi(service *service.UserService) *UserApi {

	return &UserApi{
		service: service,
	}
}

// 根据ID查询客户信息
func (this *UserApi) Find(ctx *gin.Context) {

	id, _ := strconv.ParseUint(ctx.Param("id"), 10, 64)

	news, err := this.service.FindUser(id)

	var result vo.Base

	if err != nil {
		result = constant.SERVICE_ERROR
		result.Info = err.Error()
		result.Msg = "查询新闻失败"
	} else {
		result = constant.SUCCESS
		result.Info = news
	}

	ctx.JSON(http.StatusOK, result)
}


// 分页查询客户信息
func (this *UserApi) List(ctx *gin.Context) {
	query := &form.UserPageQueryForm{}
	ctx.Bind(query)
	result := this.service.QueryUsers(query)

	ctx.JSON(http.StatusOK, result)
}

// 删除客户信息
func (this *UserApi) Delete(ctx *gin.Context) {

	id, _ := strconv.ParseUint(ctx.Param("id"), 10, 64)

	err := this.service.DeleteUsers(id)

	var result vo.Base

	if err != nil {
		result = constant.SERVICE_ERROR
		result.Info = err.Error()
		result.Msg = "删除客户失败"
	} else {
		result = constant.SUCCESS
	}

	ctx.JSON(http.StatusOK, result)
}
